package com.htao.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htao.train.common.context.LoginMemberContext;
import com.htao.train.common.resp.PageResp;
import com.htao.train.common.util.SnowUtil;
import com.htao.train.member.domain.Passenger;
import com.htao.train.member.domain.PassengerExample;
import com.htao.train.member.mapper.PassengerMapper;
import com.htao.train.member.req.PassengerQueryReq;
import com.htao.train.member.req.PassengerSaveReq;
import com.htao.train.member.resp.PassengerQueryResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author： HTTT
 * @Date： 2024/1/3 23:01
 * @Describe：
 */
@Service
public class PassengerService {
    private static final Logger LOG = LoggerFactory.getLogger(TicketService.class);
    @Resource
    private PassengerMapper passengerMapper;
    public void save(PassengerSaveReq req) {
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);
        if(ObjectUtil.isNull(req.getMemberId())){
            passenger.setMemberId(LoginMemberContext.getId());
            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passengerMapper.insert(passenger);
        }else{
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKey(passenger);
        }

    }

    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req) {
        PassengerExample passengerExample = new PassengerExample();
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        if(ObjectUtil.isNotNull(req.getMemberId())){
            criteria.andMemberIdEqualTo(req.getMemberId());
        }
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Passenger> passengers = passengerMapper.selectByExample(passengerExample);
        List<PassengerQueryResp> list = BeanUtil.copyToList(passengers, PassengerQueryResp.class);
        PageInfo<PassengerQueryResp> pageInfo = new PageInfo<>(list);
        return new PageResp<>(pageInfo.getTotal(),list);
    }

    public void delete(long id) {
        passengerMapper.deleteByPrimaryKey(id);
    }

    public List<PassengerQueryResp> queryMine() {
        PassengerExample example = new PassengerExample();
        example.setOrderByClause("name asc");
        Long id = LoginMemberContext.getId();
        LOG.info("当前登录用户id为：{}", id);
        example.createCriteria().andMemberIdEqualTo(id);
        List<Passenger> passengers = passengerMapper.selectByExample(example);
        return BeanUtil.copyToList(passengers,PassengerQueryResp.class);
    }
}
